<?php
    /*
     * post grabbed rss items in articles.
     *
    */
    
    ini_set('max_execution_time', 3600);
    error_reporting(E_ALL);
    require_once('../config/constant.php');
    require_once(DAEMONE_HOME . '/lib/lib_database.php');
	
	echo date('l jS \of F Y h:i:s A'), "||";
	
	$categories = execute_query('select * from categories');	

	echo date('l jS \of F Y h:i:s A'), "||";

	$article_limit = 3;
	while ($category = mysql_fetch_assoc($categories)) 
	{
		$item_ids = post_new_articles_using_category_id($category['category_id'], $article_limit) ;
		
		if(count($item_ids) < $article_limit)
		{
			update_old_articles_rss_publish_date($category['category_id'], $article_limit) ;
		}
		if(count($item_ids) > 0)
		{
			execute_query("insert ignore into http_cache_file (path, uri, is_file, update_action) values ('/cache/".(empty($category[slug_parent_category_name]) ? "":"$category[slug_parent_category_name]_")."$category[slug_category_name]','/article_controller/show_articles_using_category/$category[slug_category_name]/1/$category[slug_parent_category_name]',0,1)") ;
		}
	}
	execute_query("delete from rss_feed_item where grabbed_status = 3") ;
	
	echo date('l jS \of F Y h:i:s A'), "||";

	// update articles count by categories
	$result = execute_query('drop table if exists tmp_categories_count');
	$result = execute_query('create table tmp_categories_count select category_id, count(1) as article_count from articles group by category_id');
	$result = execute_query('alter table tmp_categories_count add index idx_category_id(category_id)');
	$result = execute_query('update tmp_categories_count cc, categories cg set cg.article_count=cc.article_count, cg.post_modified = now(), cg.rss_publish_date = now() where cg.category_id=cc.category_id and cg.article_count!=cc.article_count');

	echo date('l jS \of F Y h:i:s A'), "||";
	
	function post_new_articles_using_category_id($category_id, $limit)
	{
		
		$rss_items = execute_query("select item_id from rss_feed_item where category_id = $category_id and grabbed_status = 2 limit $limit ");
		$item_ids = NULL;
		while ($rss_item = mysql_fetch_assoc($rss_items)) 
		{
			$item_ids[] = $rss_item['item_id'];
		}

		if(count($item_ids) > 0)
		{
			// post new articles 
			$item_ids_list = implode(',',$item_ids);
			echo "post ($item_ids_list) cat ($category_id) <br/> /r/n";

			$result = execute_query('insert ignore into articles'
					 . ' (post_date, post_modified, rss_publish_date, post_status, slug_status, moderate_status, grabbed_status, article_title, slug_article_title, article_content, article_content_filtered, category_id, html_meta_keywords, rss_feed_item_id, grabbed_rss_url) '
					 . ' (select now(), now(), now(), 3, 1, 1, 1, title, title_slug, html_article, html_article, category_id, html_meta_keywords, item_id, article_original_url from rss_feed_item  where item_id in ('. $item_ids_list . '))');

			if($result !== FALSE) 
			{
				echo  'items posted<br/>';
				$result = execute_query("update rss_feed_item set grabbed_status = 3 where item_id in ($item_ids_list)");
			}
		}
		
		return $item_ids;
	}
	
	
	function update_old_articles_rss_publish_date($category_id, $limit)
	{
		$articles = execute_query("select article_id from articles where category_id = $category_id order by rss_publish_date limit $limit ");
		$article_ids = NULL;
		while ($article = mysql_fetch_assoc($articles)) 
		{
			$article_ids[] = $article['article_id'];
		}
		
		if(count($article_ids) > 0)
		{
			$article_ids_list = implode(',', $article_ids);
			$sql = "update articles set rss_publish_date = now() where article_id in ($article_ids_list)";
			echo  " old article($article_ids_list) cat $category_id <br/> /r/n";
			execute_query($sql);
		}
		
		return $article_ids;
	}
?>
